


function TPromise (executor){
    this.PromiseState = 'pedding';
    this.PromiseResult = null;
    this.callbacks = [];
    const self = this;
    
    function resove(data){
        if(self.PromiseState !== 'pedding'){
            return;
        }
        self.PromiseState = 'resolved';
        self.PromiseResult = data;
        self.callbacks.forEach(item => {
            item.onResolved(data)
        }) 

    }
    function reject(data){
        if(self.PromiseState !== 'pedding'){
            return;
        }
        self.PromiseState = 'rejected';
        self.PromiseResult = data;

        self.callbacks.forEach(item => {
            item.onRejected(data)
        })
    }
    try{
        executor(resove,reject)
    }catch(e){
        console.log(e);
        reject(e)
    }
   
}

TPromise.prototype.then = function(onResolved,onRejected){
    const self = this;
    //返回一个Promise
    return new TPromise((resolve,reject) => {
        if(this.PromiseState === 'resolved'){
            try {
                let result = onResolved(this.PromiseResult);
                if(result instanceof TPromise){
                    result.then(v => {
                        resolve(v);
                    }, r=> {
                        reject(r)
                    })
                }else {
                    resolve(result)
                }
            } catch (error) {
                reject(error)
            }
        }
        if(this.PromiseState === 'rejected'){
            try {
              const result = onRejected(this.PromiseResult);
              if(result instanceof TPromise){
                result.then(v => {
                    resolve(v);
                }, r=> {
                    reject(r)
                })
            }else {
                resolve(result)
            }
            } catch (error) {
                reject(error)
            }
        }

        //判断pedding的状态
        if(this.PromiseState === 'pedding'){
            //保存回调函数
            this.callbacks.push({
                onResolved: function() {
                    let result = onResolved(self.PromiseResult)
                    if(result instanceof TPromise) {
                        result.then(v => {
                            resolve(v);
                        }, r=> {
                            reject(r)
                        })
                    }else {
                        resolve(result)
                    }
                },
                onRejected: function() {
                    let result = onRejected(self.PromiseResult)
                    if(result instanceof TPromise) {
                        result.then(v => {
                            resolve(v);
                        }, r=> {
                            reject(r)
                        })
                    }else {
                        resolve(result)
                    }
                }
            })

        }

    })
}


//用法 测试
let p =new TPromise((resove,reject)=>{
    setTimeout(()=>{
        resove('这是成功')
    },1000)
   
})
p.then((data)=>{
    console.log('成功=',data);
    return new TPromise((resolve,reject)=>{
        setTimeout(()=>{
            resolve('这是成功2')
        })
    })
},(reason)=>{
    console.log('失败=',reason);
}).then((data)=>{
    console.log('成功=',data);
    return '这是成功3'
}).then((data)=>{
    console.log('成功=',data);
})